/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package log2excel.ViewInterface;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import log2excel.model.ExtentionUsernameRecord;
import log2excel.model.TrunkLineRecord;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import log2excel.model.HLRRecord;
import log2excel.model.OutgoingCallReport;

/**
 *
 * @author aegish
 */
public class TableAccess {
    
    public static ArrayList<ExtentionUsernameRecord> getExtentionUsernameRecords(){
        ArrayList<ExtentionUsernameRecord> arrayList = new ArrayList<ExtentionUsernameRecord>();
        int row = log2excel.Log2ExcelView.jTableModel1.getRowCount();
        for (int i = 0; i < row; i++){
            String ext = log2excel.Log2ExcelView.jTableModel1.getValueAt(i, 0) != null ? 
                    log2excel.Log2ExcelView.jTableModel1.getValueAt(i, 0).toString() : "";
            String username = log2excel.Log2ExcelView.jTableModel1.getValueAt(i, 1) != null ? 
                    log2excel.Log2ExcelView.jTableModel1.getValueAt(i, 1).toString() : "";
            ExtentionUsernameRecord eur = new ExtentionUsernameRecord(ext, username);
            if (ext.length() > 0 && username.length() > 0)
                arrayList.add(eur);
        }
        return arrayList;
    }
    
    public static void saveExtentionUsernameRecords(ArrayList<ExtentionUsernameRecord> arraylist){
        log2excel.Log2ExcelView.jTableModel1.setRowCount(0);
        for (int i = 0; i < arraylist.size(); i++){
            ExtentionUsernameRecord obj = arraylist.get(i);
            log2excel.Log2ExcelView.jTableModel1.addRow(obj.toArray());
        }
    }
    
    public static ArrayList<TrunkLineRecord> getTrunkLineRecords(){
        ArrayList<TrunkLineRecord> arrayList = new ArrayList<TrunkLineRecord>();
        int row = log2excel.Log2ExcelView.jTableModel2.getRowCount();
        for (int i = 0; i < row; i++){
            String trk = log2excel.Log2ExcelView.jTableModel2.getValueAt(i, 0) != null ? 
                    log2excel.Log2ExcelView.jTableModel2.getValueAt(i, 0).toString() : "";
            String line = log2excel.Log2ExcelView.jTableModel2.getValueAt(i, 1) != null ? 
                    log2excel.Log2ExcelView.jTableModel2.getValueAt(i, 1).toString() : "";
            TrunkLineRecord tlr = new TrunkLineRecord(trk, 
                    line);
            if (trk.length() > 0 && line.length() > 0)
                arrayList.add(tlr);
        }
        return arrayList;
    }
    
    public static void saveTrunkLineRecords(ArrayList<TrunkLineRecord> arraylist){
        log2excel.Log2ExcelView.jTableModel2.setRowCount(0);
        for (int i = 0; i < arraylist.size(); i++){
            TrunkLineRecord obj = arraylist.get(i);
            log2excel.Log2ExcelView.jTableModel2.addRow(obj.toArray());
        }
    }
    
    public static ArrayList<HLRRecord> getHLRRecords(){
        ArrayList<HLRRecord> arrayList = new ArrayList<HLRRecord>();
        int row = log2excel.Log2ExcelView.jTableModel3.getRowCount();
        for (int i = 0; i < row; i++){
            String hlrs = log2excel.Log2ExcelView.jTableModel3.getValueAt(i, 0) != null ? 
                    log2excel.Log2ExcelView.jTableModel3.getValueAt(i, 0).toString() : "";
            String op = log2excel.Log2ExcelView.jTableModel3.getValueAt(i, 1) != null ? 
                    log2excel.Log2ExcelView.jTableModel3.getValueAt(i, 1).toString() : "";
            String loc = log2excel.Log2ExcelView.jTableModel3.getValueAt(i, 2) != null ? 
                    log2excel.Log2ExcelView.jTableModel3.getValueAt(i, 2).toString() : "";
            String cost = log2excel.Log2ExcelView.jTableModel3.getValueAt(i, 3) != null ? 
                    log2excel.Log2ExcelView.jTableModel3.getValueAt(i, 3).toString() : "";
            String pulse = log2excel.Log2ExcelView.jTableModel3.getValueAt(i, 4) != null ? 
                    log2excel.Log2ExcelView.jTableModel3.getValueAt(i, 4).toString() : "";
            String type = log2excel.Log2ExcelView.jTableModel3.getValueAt(i, 5) != null ? 
                    log2excel.Log2ExcelView.jTableModel3.getValueAt(i, 5).toString() : "";
            long costl = 0; 
            long pulsel = 0;
            try{
                costl = Long.parseLong(cost);
            }catch (NumberFormatException ne){
            }
            try{
                pulsel = Long.parseLong(pulse);
            }catch (NumberFormatException ne){
            }
            HLRRecord hlr = new HLRRecord(hlrs, op, loc, costl, pulsel, type );
            if (hlr.HLR.length() > 0)
                arrayList.add(hlr);
        }
        return arrayList;
    }
    
    public static void saveHLRRecords(ArrayList<HLRRecord> arraylist){
        log2excel.Log2ExcelView.jTableModel3.setRowCount(0);
        for (int i = 0; i < arraylist.size(); i++){
            HLRRecord obj = arraylist.get(i);
            log2excel.Log2ExcelView.jTableModel3.addRow(obj.toArray());
        }
    }
    public static ArrayList<OutgoingCallReport> getOutgoingCallReport(){
        ArrayList<OutgoingCallReport> arrayList = new ArrayList<OutgoingCallReport>();
        int row = log2excel.Log2ExcelView.jTableModel4.getRowCount();
        for (int i = 0; i < row; i++){
            String srn = log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 0) != null ? 
                    log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 0).toString() : "";
            String stn = log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 1) != null ? 
                    log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 1).toString() : "";
            String trk = log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 2) != null ? 
                    log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 2).toString() : "";
            String calledNo = log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 3) != null ? 
                    log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 3).toString() : "";
            String date = log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 4) != null ? 
                    log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 4).toString() : "";
            String time = log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 5) != null ? 
                    log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 5).toString() : "";
            String duration = log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 6) != null ? 
                    log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 6).toString() : "";
            String unit = log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 7) != null ? 
                    log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 7).toString() : "";
            String amount = log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 8) != null ? 
                    log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 8).toString() : "";
            String r = log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 9) != null ? 
                    log2excel.Log2ExcelView.jTableModel4.getValueAt(i, 9).toString() : "";
            int srn1 = 0; 
            int stn1 = 0;
            int trk1 = 0;
            DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
            DateFormat tf = new SimpleDateFormat("HH:mm:ss");
            Date date1 = new Date();
            Date time1 = new Date();
            int duration1 = 0;
            int unit1 = 0;
            double amount1 = 0;
            try{
                srn1 = Integer.parseInt(srn);
            }catch (NumberFormatException ne){
            }
            try{
                stn1 = Integer.parseInt(stn);
            }catch (NumberFormatException ne){
            }
            try{
                trk1 = Integer.parseInt(trk);
            }catch (NumberFormatException ne){
            }
            try {
                date1 = df.parse(date);
            } catch (ParseException ex) {
                Logger.getLogger(TableAccess.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                time1 = tf.parse(time);
            } catch (ParseException ex) {
                Logger.getLogger(TableAccess.class.getName()).log(Level.SEVERE, null, ex);
            }
            try{
                duration1 = Integer.parseInt(duration);
            }catch (NumberFormatException ne){
            }
            try{
                unit1 = Integer.parseInt(unit);
            }catch (NumberFormatException ne){
            }
            try{
                amount1 = Double.parseDouble(amount);
            }catch (NumberFormatException ne){
            }
            OutgoingCallReport ocr = new OutgoingCallReport(srn1, stn1, trk1, calledNo, date1,time1, duration1, unit1, amount1, r );
            arrayList.add(ocr);
        }
        return arrayList;
    }
    
    public static void saveOutgoingCallReport(ArrayList<OutgoingCallReport> arraylist){
        log2excel.Log2ExcelView.jTableModel4.setRowCount(0);
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        SimpleDateFormat tf = new SimpleDateFormat("HH:mm:ss");
        ArrayList<ExtentionUsernameRecord> extentionUsernameRecords = getExtentionUsernameRecords();
        ArrayList<TrunkLineRecord> trunkLineRecords = getTrunkLineRecords();
        
        for (int i = 0; i < arraylist.size(); i++){
            OutgoingCallReport obj = arraylist.get(i);
            String username = "";
            String line = "";
            for (Iterator<ExtentionUsernameRecord> it = extentionUsernameRecords.iterator(); it.hasNext();) {
                ExtentionUsernameRecord record = it.next();
                if (record.Extention.equals(""+obj.stn)){
                    username = record.Username;
                    break;
                }
            }
            for (Iterator<TrunkLineRecord> it = trunkLineRecords.iterator(); it.hasNext();) {
                TrunkLineRecord trunkLineRecord = it.next();
                if (trunkLineRecord.Trunk.equals(""+obj.trk)){
                    line = trunkLineRecord.Line;
                    break;
                }
            }
            Object[] row = new Object[]{
                obj.srn,
                ""+obj.stn,
                username,
                ""+obj.trk,
                line,
                obj.calledNo, 
                df.format(obj.date), 
                tf.format(obj.time), 
                ""+obj.duration,  
                ""+obj.unit, 
                ""+obj.amount, 
                obj.r
            };
            log2excel.Log2ExcelView.jTableModel4.addRow(row);
        }
    }
}
